package org.gd.hellojaxb.adapters;

import javax.xml.bind.annotation.adapters.XmlAdapter;

public class DoubleAdaptersFactory {

    public static String getFormattedAmount(Double amount) {
        return String.format("%.2f", amount);
    }

    public static final class Amount extends XmlAdapter<String, Double> {
        @Override
        public Double unmarshal(String v) throws Exception {
            if(v == null) {
                return null;
            }
            return Double.parseDouble(v);
        }
        @Override
        public String marshal(Double v) throws Exception {
            if(v == null) {
                return null;
            }
            return getFormattedAmount(v);
        }
    }
}
